package com.tgpay.iot.controller;

import com.tgpay.iot.dto.ChargingDto;
import com.tgpay.iot.model.ApiResponse;
import com.tgpay.iot.service.ChargingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;

/**
 * 充电控制器
 */
@RestController
@RequestMapping("/api/charging")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin(origins = "*")
public class ChargingController {
    
    private final ChargingService chargingService;
    
    /**
     * 启动充电
     */
    @PostMapping("/start")
    public ApiResponse<ChargingDto.StartChargingResponse> startCharging(
            @Valid @RequestBody ChargingDto.StartChargingRequest request,
            HttpServletRequest httpRequest) {
        try {
            // 验证签名（可选）
            // if (!verifyRequestSignature(httpRequest, request)) {
            //     return ApiResponse.error("签名验证失败");
            // }
            
            ChargingDto.StartChargingResponse response = chargingService.startCharging(request);
            return ApiResponse.success(response);
        } catch (Exception e) {
            log.error("启动充电失败", e);
            return ApiResponse.error("启动充电失败：" + e.getMessage());
        }
    }
    
    /**
     * 停止充电
     */
    @PostMapping("/stop")
    public ApiResponse<Void> stopCharging(
            @Valid @RequestBody ChargingDto.StopChargingRequest request,
            HttpServletRequest httpRequest) {
        try {
            // 验证签名（可选）
            // if (!verifyRequestSignature(httpRequest, request)) {
            //     return ApiResponse.error("签名验证失败");
            // }
            
            chargingService.stopCharging(request);
            return ApiResponse.success();
        } catch (Exception e) {
            log.error("停止充电失败", e);
            return ApiResponse.error("停止充电失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取充电状态
     */
    @GetMapping("/status")
    public ApiResponse<ChargingDto.ChargingStatusResponse> getChargingStatus(
            @RequestParam String orderNo,
            HttpServletRequest request) {
        try {
            // 验证签名（可选）
            // if (!verifyRequestSignature(request)) {
            //     return ApiResponse.error("签名验证失败");
            // }
            
            ChargingDto.ChargingStatusResponse response = chargingService.getChargingStatus(orderNo);
            return ApiResponse.success(response);
        } catch (Exception e) {
            log.error("获取充电状态失败，订单号：{}", orderNo, e);
            return ApiResponse.error("获取充电状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询充电订单
     */
    @GetMapping("/orders")
    public ApiResponse<java.util.List<com.tgpay.iot.entity.ChargingOrder>> getChargingOrders(
            @RequestParam String imei,
            @RequestParam Integer portId,
            @RequestParam(required = false) Integer status,
            HttpServletRequest request) {
        try {
            // 验证签名（可选）
            // if (!verifyRequestSignature(request)) {
            //     return ApiResponse.error("签名验证失败");
            // }
            
            java.util.List<com.tgpay.iot.entity.ChargingOrder> orders = chargingService.getChargingOrders(imei, portId, status);
            return ApiResponse.success(orders);
        } catch (Exception e) {
            log.error("查询充电订单失败，设备：{}，端口：{}，状态：{}", imei, portId, status, e);
            return ApiResponse.error("查询充电订单失败：" + e.getMessage());
        }
    }
    

}